import java.util.ArrayList;
import java.util.List;

class Solution {
    public   String longestCommonPrefix(String[] strs) {
        if(strs.length == 0) {
            return "";
        }
        String ans = strs[0];
        for(int i =1;i<strs.length;i++) {
            int j=0;
            for(;j<ans.length() && j < strs[i].length();j++) {
                if(ans.charAt(j) != strs[i].charAt(j)){
                    break;
                }
            }
            ans = ans.substring(0, j);
            if(ans.equals(""))
                return ans;
        }
        return ans;
    }
    public List<Integer> spiralOrder(int[][] matrix) {
        int left=0,right=matrix[0].length-1;
        int up=0,down=matrix.length-1;
        List<Integer> list=new ArrayList<>();
        while(left<=right&&up<=down){
            for(int i=left;i<=right;i++){
                list.add(matrix[up][i]);
            }
            up++;
            if(up>down){
                break;
            }
            for(int j=up;j<=down;j++){
                list.add(matrix[j][right]);
            }
            right--;
            if(left>right){
                break;
            }
            for(int m=right;m>=left;m--){
                list.add(matrix[down][m]);
            }
            down--;
            if(up>down){
                break;
            }
            for(int n=left;n<=right;n++){
                list.add(matrix[up][n]);
            }
            left++;
            if(left>right){
                break;
            }
        }
        return list;
    }
}